const app = getApp();
Page({
  data: {
    StatusBar: app.globalData.StatusBar,
    CustomBar: app.globalData.CustomBar,
    index: null,
    picker: ['酒水饮料', '糖果饼干', '医疗保健', '数码家电', '母婴玩具'],
    imgList: [],
    modalName: null,
    itemValue: null,
    textareaAValue: '',
    textareaBValue: '',
    unit: "选择",
    scode: "",
    goodsName: "",
    simpleName: "",
    location: "",
    retailPrice: "",
    costPrice: "",
    quantity: "",
    className: "",
    items: [{
        name: 'ge',
        value: '个'
      },
      {
        name: 'zhi',
        value: '只'
      },
      {
        name: 'bao',
        value: '包'
      },
      {
        name: 'tiao',
        value: '条'
      },
      {
        name: 'ba',
        value: '把'
      },
      {
        name: 'zhi',
        value: '支'
      },
    ],
    unit: null
  },
  onLoad:function(e) {
    wx.showLoading({
      title: '正在加载',
    })
    var goodData = JSON.parse(e.goodData);
    var that = this;
    var image = this.data.imgList;
    image.push(goodData.photo);
    console.log(goodData)
    this.setData({
      goodData: goodData,
      imgList:image,
    })

    wx.request({
      url: "http://47.106.145.141:8080/api/v1/type",
      method: "GET",
      data: {
        "token": app.globalData.key,
        "shopId": app.globalData.shopId,
      },
      success: function (result) {
        console.log(result.data.dataInfo);
        that.setData({
          picker: result.data.dataInfo,
        })
      }
    })

    wx.hideLoading();
  },

  PickerChange(e) {
    console.log(e);
    this.setData({
      index: e.detail.value,
    })
  },
  MultiChange(e) {
    this.setData({
      multiIndex: e.detail.value
    })
  },
  ChooseImage() {
    wx.chooseImage({
      count: "1", //默认1
      sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album'], //从相册选择
      success: (res) => {
        if (this.data.imgList.length != 0) {
          this.setData({
            imgList: this.data.imgList.concat(res.tempFilePaths)
          })
        } else {
          this.setData({
            imgList: res.tempFilePaths
          })
        }
      }
    });
  },
  ViewImage(e) {
    wx.previewImage({
      urls: this.data.imgList,
      current: e.currentTarget.dataset.url
    });
  },
  DelImg(e) {
    wx.showModal({
      title: '删除图片',
      content: '确定要删除这张图片吗？',
      cancelText: '取消',
      confirmText: '确定',
      success: res => {
        if (res.confirm) {
          this.data.imgList.splice(e.currentTarget.dataset.index, 1);
          this.setData({
            imgList: this.data.imgList
          })
        }
      }
    })
  },
  textareaAInput(e) {
    this.setData({
      textareaAValue: e.detail.value
    })
  },
  textareaBInput(e) {
    this.setData({
      textareaBValue: e.detail.value
    })
  },
  showModal(e) {
    this.setData({

      modalName: e.currentTarget.dataset.target,

    })
  },
  hideModal(e) {
    this.setData({
      modalName: null,

    })
  },
  ChooseCheckbox(e) {
    let items = this.data.checkbox;
    let values = e.currentTarget.dataset.value;
    for (let i = 0, lenI = items.length; i < lenI; ++i) {
      if (items[i].value == values) {
        items[i].checked = !items[i].checked;
        break
      }
    }
    this.setData({
      checkbox: items
    })
  },
  //提交表单，存储数据
  formSubmit: function(e) {

    var form = e.detail.value;
    var gooddata = this.data.goodData;

    console.log(form);
    console.log(gooddata);

    if (form.goodsName) {
      console.log("非空")
    } else{
      form.goodsName = gooddata.fullName;
    }

    if (form.goodsName) {
      console.log("非空")
    } else {
      form.goodsName = gooddata.fullName;
    }

    if (form.scode) {
      console.log("非空")
    } else {
      form.scode = gooddata.barCode;
    }

    if (form.costPrice) {
      console.log("非空")
    } else {
      form.costPrice = gooddata.primePrice;
    }

    if (form.location) {
      console.log("非空")
    } else {
      form.location = gooddata.place;
    }

    if (form.quantity) {
      console.log("非空")
    } else {
      form.quantity = gooddata.quantity;
    }

    if (form.retailPrice) {
      console.log("非空")
    } else {
      form.retailPrice = gooddata.retailPrice;
    }

    if (form.simpleName) {
      console.log("非空")
    } else {
      form.simpleName = gooddata.shortForm;
    }
    console.log(form);
    console.log(gooddata.id);
    console.log(app.globalData.key);

    wx.request({
      url: "http://47.106.145.141:8080/api/v1/goods",
      method: "PUT",
      data: {
        "token": app.globalData.key,
        "id": gooddata.id,
        "fullName": form.goodsName,
        "shortForm": form.simpleName,
        "retailPrice": form.retailPrice,
        "unit": form.unit,
        "place": form.location
      },
      header: {
        "content-type": "application/x-www-form-urlencoded"
      },
      success: function (result) {
        console.log(result.data);
      }
    })

    var pages = getCurrentPages();
    var goodpage = pages[pages.length-3];

    goodpage.setData({
      Refresh:true,
    })
    console.log(goodpage.data.Refresh);
    wx.navigateBack({
      delta: 2
    })


  },




  radioChange: function(e) {
    
    this.setData({
      'goodData.unit': e.detail.value,
    })
  },
})